package com.ciwong.epaper.util;

import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.modules.evaluate.bean.Sentence;
import com.ciwong.epaper.modules.evaluate.bean.WordDetail;
import com.ciwong.epaper.modules.evaluate.util.SpeechUtils;
import com.ciwong.libs.oralevaluate.bean.EvaluateResult;
import com.ciwong.libs.oralevaluate.bean.Word;

import java.util.List;

/**
 * Created by keepon on 2018/6/25.
 */
public class WordUtil {

    public static Sentence dealWordColoration(EvaluateResult evaluateResult, Sentence sentence) {
        try {
            sentence.setScore((int) evaluateResult.getScore());
            List<WordDetail> words = sentence.getWordDetails();
            List<Word> gradeWords = evaluateResult.getWords();

            if (gradeWords != null && words != null) {
                //单词长度为1时取外面那个分数
                if (gradeWords.size() == 1 && words.size() == 1) {
                    Word gradeWord = gradeWords.get(0);
                    WordDetail word = words.get(0);
//					if (SpeechUtils.replace(word.getText()).equals(gradeWord.getText())) {
                    int color;
                    word.setScore(evaluateResult.getScore());
                    if (evaluateResult.getScore() < SpeechUtils.SCORE_6) {
                        color = R.color.me_item_name_sub_txt_color;
                    }else if (evaluateResult.getScore() < SpeechUtils.SCORE_2) {
                        color = R.color.score_red;
                    } else if (evaluateResult.getScore() < SpeechUtils.SCORE_4) {
                        color = R.color.score_yellow;
                    } else {
                        color = R.color.score_green_word;
                    }
                    word.setColor(EApplication.getInstance().getResources().getColor(color));

//					} else {
//						FeedbackUtil.getInstance().addFeedbackLog(FeedbackUtil.FEEDBACK_TYPE_WORD_SPEECH, "word.getText()==" + word.getText() + "  gradeWord.getText()==" + gradeWord.getText() + " 替换后=" + SpeechUtils.replace(word.getText()), "gradeWord和word不一致 ");
//					}
                } else {
                    //单词为词组时则遍历里面的word返回的分数
                    for (WordDetail w : words) {
                        for (Word gradeWord : gradeWords) {
                            if (SpeechUtils.replace(w.getText()).equals(gradeWord.getText())) {
                                w.setScore(gradeWord.getScore());
                                int color;
                                if (w.getScore() < SpeechUtils.SCORE_6) {
                                    color = R.color.me_item_name_sub_txt_color;
                                }else if (w.getScore() < SpeechUtils.ONE_SCORE_2) {
                                    color = R.color.score_red;
                                } else if (w.getScore() < SpeechUtils.ONE_SCORE_4) {
                                    color = R.color.score_yellow;
                                } else {
                                    color = R.color.score_green_word;
                                }
                                w.setColor(EApplication.getInstance().getResources().getColor(color));
                                break;
                            } else {
                                w.setColor(EApplication.getInstance().getResources().getColor(R.color.repeat_read_gray));
                            }
                        }

                    }

                }
            }
            sentence.setHasGrade(true);
        } catch (Exception e) {

        }

        return sentence;
    }
}
